//
//  addItemTableViewController.swift
//  ToDoList
//
//  Created by Qingke on 16/4/12.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

protocol addItemdelegate:class { //只有类才能用weak 的属性
    func addItemFunc (controller: addItemTableViewController ,didFinishAdd task :Task)
    func addItemFunc (controller: addItemTableViewController ,didFinishedit task :Task, indePathOfCell indexPath: NSIndexPath )
}

class addItemTableViewController: UITableViewController {
    weak var addItemFunc1 : addItemdelegate?
    weak var retask : Task?
    weak var indexOftask: NSIndexPath?
    @IBOutlet weak var textFild: UITextField!
    
    @IBOutlet weak var saveButton: UIBarButtonItem!
    override func viewDidLoad() {
        super.viewDidLoad()
        saveButton.enabled = false
        
        if let retask1 = retask {
            textFild.text = retask1.name
            self.navigationItem.title = "编辑任务"
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidAppear(animated: Bool) {
        textFild.becomeFirstResponder()
        
    }
    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }

    
    //防止cell被选中的两个方法： 1 是在tableView中的selected 选择unselected 。2 用代理willseleted 就是被选中前的 返回nil
    override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
        return nil
    }
    /*
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

        // Configure the cell...

        return cell
    }
    */

    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    
    
    //MARK: action 
    @IBAction func cancelButton (sender: UIBarButtonItem){
        self.dismissViewControllerAnimated(true, completion: nil)
    
    }
    @IBAction func saveButtonAction(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil)
        let str: String? = textFild.text
        let task = Task(name: str!)
        print(str)
        if let _ = retask, let indexOftask = indexOftask{
            addItemFunc1?.addItemFunc(self, didFinishedit: task ,indePathOfCell: indexOftask)
        } else {
            addItemFunc1?.addItemFunc(self, didFinishAdd: task)
        }

    }
    

}
extension addItemTableViewController : UITextFieldDelegate {
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        let oldText : NSString = textFild.text!
        let newText : NSString = oldText.stringByReplacingCharactersInRange(range, withString: string)
        if newText.length > 0 {
            saveButton.enabled = true
        }
        return true
    }
    //这句话的意思可以理解为， 当我按下Return 时候。 这个Return是否有效， 有效就是turn  无效就是false.
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        //textFild.resignFirstResponder()
        return false
    }
    
    
}
